/*年轻人，只管向前看，不要管自暴自弃者的话*/
package com.composetest.page.splash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.composetest.R
import com.composetest.ui.theme.ComposeTestTheme

/**
 * create by itz on 2024/10/10 15:59
 * desc :
 **/

@Composable
fun SplashRoute(toGuide: () -> Unit, toMain: () -> Unit) {
    // 创建ViewModel
    val viewModel: SplashViewModel = viewModel()

    // 需要观察的数据
    val timeDown = viewModel.timeDown
    val navigationGuide = viewModel.navigationGuide

    SplashScreen(
        timeDown = timeDown,
        navigationGuide = navigationGuide,
        toGuide = toGuide,
        toMain = toMain
    )
}

@Composable
fun SplashScreen(
    timeDown: Long = 0L,
    navigationGuide: Boolean = false,
    toGuide: () -> Unit = {},
    toMain: () -> Unit = {}
) {

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_logo_splash),
            contentDescription = null,
            modifier = Modifier
                .align(
                    Alignment.Center
                )
                .padding(bottom = 150.dp)
        )
        Text(
            text = "版权号：2024",
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 30.dp),
            style = TextStyle(fontSize = 14.sp, color = Color.Gray)
        )

        Button(
            onClick = toMain, modifier = Modifier
                .align(Alignment.TopEnd)
                .padding(top = 16.dp, end = 16.dp)
        ) {
            Text(
                text = "跳过：$timeDown",
                style = TextStyle(fontSize = 14.sp)
            )
        }

    }

    if (navigationGuide) {
        // 只在首次创建时执行内部函数
        LaunchedEffect(key1 = true) {
            // toGuide()
            toMain()
        }
    }

}

@Preview
@Composable
fun SplashScreenPreview() {
    ComposeTestTheme {
        SplashScreen()
    }
}